package com.ebc.eservice.service.app.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ebc.eservice.mapper.SingleIntroductionMapper;
import com.ebc.eservice.pojo.EbcCompanyArticle;
import com.ebc.eservice.pojo.SingleIntroduction;
import com.ebc.eservice.pojo.pic.UploadFile;
import com.ebc.eservice.service.app.AppIntroductService;
import com.ebc.eservice.util.JsonResult;
import com.ebc.eservice.vo.Intro.IntroductionRespPicVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * @author: Joey
 * @Description: 个性化简介-app
 * @date:2022/9/26 17:55
 */
@Service
public class IntroductAppImp implements AppIntroductService {

    @Autowired
    private SingleIntroductionMapper IntroductionMapper;
    @Autowired
    private MongoTemplate mongoTemplate;

    @Override
    public JsonResult getIntroList(Integer companyId) {
        QueryWrapper<SingleIntroduction> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("open",1);
        queryWrapper.eq("company_id",companyId);
        List<SingleIntroduction> introlist = IntroductionMapper.selectList(queryWrapper);

        List<Integer> picIdList = new ArrayList<>();
        for (SingleIntroduction intro : introlist) {
            picIdList.add(intro.getId());
        }
        List<IntroductionRespPicVo> picData = new ArrayList<>();
        IntroductionRespPicVo respPicVo=null;
        for (Integer introId : picIdList) {
            respPicVo = new IntroductionRespPicVo();
            byte[] dataArt = null;
            //条件查询器
            Query query = Query.query(Criteria.where("introId").is(introId));
            UploadFile file = mongoTemplate.findOne(query,UploadFile.class);
            if(file != null){
                dataArt = file.getContent().getData();//blob类型
                respPicVo.setId(introId);
                respPicVo.setDataIntro(dataArt);
                picData.add(respPicVo);
            }

        }
        HashMap<String, Object> map = new HashMap<>();
        map.put("IntroList",introlist);
        map.put("IntroPic",picData);
        return JsonResult.success(map);
    }
}
